const mongoose = require('mongoose');
const { v4: uuidv4 } = require('uuid');

const menuSchema = new mongoose.Schema({
    id: {
        type: String,
        required: true,
        unique: true,
        default: () => uuidv4(),
        description: 'id'
    },
    parent_id: {
        type: String,
        default: '',
        description: '父级ID'
    },
    title: {
        type: String,
        default: '',
        description: '菜单标题'
    },
    path: {
        type: String,
        default: '',
        description: '菜单路径'
    },
    icon: { 
        type: String,
        default: '',
        description: '菜单图标'
    },
    level: {
        type: Number,
        enum: [0, 1, 2],  // 0: 目录, 1: 菜单, 2: 按钮
        default: 1,
        required: [true, '菜单类型必填'],
        description: '菜单类型'
    },
    permission: {
        type: String,
        trim: true,
        default: '',  // 权限标识，如：system:user:add
        description: '权限标识'

    },
    sort: {
        type: Number,
        default: 0,
        description: '排序'
    },
    hidden: {
        type: Boolean,
        default: false,
        description: '是否隐藏'
    }
}, {
    timestamps: true,
    versionKey: false
});

// 创建索引
menuSchema.index({ id: 1 }, { unique: true });
menuSchema.index({ parent_id: 1 });
menuSchema.index({ type: 1 });

module.exports = mongoose.model('Menu', menuSchema); 